package com.wang.tasker.plug.net;

import java.util.Date;
import java.util.Properties;

import javax.mail.Authenticator;
import javax.mail.BodyPart;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Multipart;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;

public class EmailT {
	
	   private String host;  
	    private String auth;  
	    private String username;  
	    private String domainUser;  
	    private String password;  
	  
	    public boolean send(String[] to, String[] cc, String[] bcc, String subject, String content) throws MessagingException {  
	        Properties props = new Properties();  
	        props.put("mail.smtp.host", host);  
	        props.setProperty("mail.imaps.socketFactory.port", "443");
	        props.setProperty("mail.imaps.starttls.enable", "true");
	        props.setProperty("mail.smtp.ssl.enable","true");
	        props.setProperty("mail.imaps.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
	        props.setProperty("mail.imaps.socketFactory.fallback", "false");
	        props.setProperty("mail.imaps.auth.plain.disable", "true");
	        props.setProperty("mail.imaps.auth.ntlm.disable", "true");
	        props.setProperty("mail.imaps.auth.gssapi.disable", "true");
//	        props.setProperty("username", username);   
//	        props.setProperty("password", password);
//	        props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
//	        props.put("mail.smtp.socketFactory.fallback", "false");
//	        props.put("mail.smtp.port", "443");
//	        props.put("mail.smtp.socketFactory.port", "443");
	        props.put("mail.smtp.starttls.enable","true");
	        props.put("mail.smtp.auth", auth);  
//	        Session s = Session.getInstance(props); 
	        Session s = Session.getDefaultInstance(props, new Authenticator(){
	        	protected PasswordAuthentication getPasswordAuthentication() {
	        	return new PasswordAuthentication(username, password);
	        	}});
	              s.setDebug(true);  
	        
//	        s.setDebug(true);
	  
	        MimeMessage message = new MimeMessage(s);  
	  
	        InternetAddress from = new InternetAddress(username);  
	        message.setFrom(from);  
	        InternetAddress[] Toaddress = new InternetAddress[to.length];  
	        for (int i = 0; i < to.length; i++)  
	            Toaddress[i] = new InternetAddress(to[i]);  
	        message.setRecipients(Message.RecipientType.TO, Toaddress);  
	  
	        if (cc != null) {  
	            InternetAddress[] Ccaddress = new InternetAddress[cc.length];  
	            for (int i = 0; i < cc.length; i++)  
	                Ccaddress[i] = new InternetAddress(cc[i]);  
	            message.setRecipients(Message.RecipientType.CC, Ccaddress);  
	        }  
	  
	        if (bcc != null) {  
	            InternetAddress[] Bccaddress = new InternetAddress[bcc.length];  
	            for (int i = 0; i < bcc.length; i++)  
	                Bccaddress[i] = new InternetAddress(bcc[i]);  
	            message.setRecipients(Message.RecipientType.BCC, Bccaddress);  
	        }  
	        message.setSubject(subject);  
	        message.setSentDate(new Date());  
	  
	        BodyPart mdp = new MimeBodyPart();  
	        mdp.setContent(content, "text/html;charset=utf-8");  
	        Multipart mm = new MimeMultipart();  
	        mm.addBodyPart(mdp);  
	        message.setContent(mm);  
	  
	        message.saveChanges();  
//	        Store transport = s.getStore("imap");
	        Transport transport = s.getTransport("smtp");
	        transport.connect(host, 443, (null == domainUser) ? username : domainUser, password);  
//	        transport.sendMessage(message, message.getAllRecipients());  
	        transport.close();  
	        return true;  
	    }  
	  
	    public EmailT(String host, String auth, String domainUser, String username, String password) {  
	        super();  
	        this.host = host;  
	        this.auth = auth;  
	        this.domainUser = domainUser;  
	        this.username = username;  
	        this.password = password;  
	    }  
	    

	/**
	 * @param args
	 * @throws MessagingException 
	 */
	public static void main(String[] args) throws MessagingException {
		System.out.println("=============");
		new EmailT("c1ex10cas01.concept1.com", "true", "concept1\\cognos", "cognos@concept1.com", "cognos321").send(new String[] { "wang@concept1.com" }, null, null, "demo_title", "<h3>test</h3>");  
		// TODO Auto-generated method stub
//		Properties props = new Properties();
//
//		props.put("mail.smtp.socketFactory.class",
//				"javax.net.ssl.SSLSocketFactory");
//		props.put("mail.smtp.socketFactory.fallback", "false");
//
//		props.put("mail.smtp.starttls.enable", "true");
//		props.put("mail.smtp.auth", "true");
//
//		Session s = Session.getDefaultInstance(props, new Authenticator() {
//			protected PasswordAuthentication getPasswordAuthentication() {
//				return new PasswordAuthentication("concept1\\wzhenzhong",
//						"wang321");
//			}
//		});
//		s.setDebug(true);
//
//		MimeMessage message = new MimeMessage(s);
//		message.saveChanges();
//		Transport transport = s.getTransport("smtp");
//		
//		transport.connect("c1ex10cas01.concept1.com", 443,"concept1\\wzhenzhong", "wang321");  
//
//		transport.close();  
		
	}

}
